// 请求json 文件的数据，放入收获地址里
async function getARData() {
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // ++++++++++++++++++"请求json 文件的数据，放入收获地址里"+++++++++++
    console.log("请求json 文件的数据，放入收获地址里")
    const result = await ajax1Promise("http://localhost:3000/getuserdata");
    let res_data = JSON.parse(result).data;
    let str = "";
    console.log(res_data, 123)
    let address_divOBJ = document.querySelector(".address_div");
    document.querySelector(".count").innerHTML = res_data.length;
    console.log(address_divOBJ)
    res_data.forEach(element => {
        str += `<div class="addDiv">
                    <p class="ff">设为默认地址</p>
                    <div>
                        <p><i class="iconfont icon-geren"></i> ${element.name}</p>
                        <p><i class="iconfont icon-dingwei"></i>${element.add.sheng}${element.add.shi}${element.add.xian}${element.add.anytail}
                        </p>
                        <p><span>(不支持退换货)</span></p>
                        <p><i class="iconfont icon-shouji">
                        </i> <span> ${element.cell}</span>
                            <button class="bnt">修改</button>
                        </p>
                    </div>
                    </div>`
    });
    address_divOBJ.innerHTML = str;    //地址数据的嵌入
    document.querySelector(".addDiv").className = "addDiv addSel";   //设置第一个的背景与众不同
    let obj1 = document.querySelector(".addDiv p");    //设置第一个为默认地址
    obj1.className = "ff ffid"
    obj1.innerText = "默认地址"
    // 独立的div addDiv assSel
    // 默认地址  ff ffid

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    addressFile();  //收获地址数据样式
    fnnnnn();


}

// 跳到指定位置，而且信息整合
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function fnnnnn() {
    console.log(12)
    let bntobj = document.querySelectorAll(".address_div .addDiv .bnt");
    console.log(bntobj)
    bntobj.forEach((item, index) => {
        item.onclick = function () {
            let m = document.querySelector(".address_div").clientHeight;
            let p = document.querySelector(".rail_div").clientHeight;
            document.documentElement.scrollTop = document.body.scrollTop = p + m + 300;
            async function fn5() {
                const result = await ajax1Promise("http://localhost:3000/getuserdata");
                let res_data = JSON.parse(result).data;
                console.log(res_data[index])
                document.querySelector(".getname").value = res_data[index].name;    //名字
                document.querySelector(".getphone").value = res_data[index].cell //手机号
                console.log(res_data[index].add.anytail)
                document.querySelector(".getanytail").value = res_data[index].add.anytail;   //详细
            }
            fn5();
        }
    })
}



// *************************************************************************************************
// 对地址栏数据的更改，鼠标移入事件，点击切换默认地址
async function addressFile() {
    console.log("对地址栏数据的更改，鼠标移入事件，点击切换默认地址")
    let addDivobj = document.querySelectorAll(".address_div .addDiv");
    let ffobj = document.querySelectorAll(".addDiv .ff"); //所有默认地址
    console.log(ffobj, 1234)
    let ffobjid = document.querySelector(".ffid");
    // 鼠标移入 默认地址出现
    addDivobj.forEach((item, index) => {
        item.onmouseover = function () {
            ffobj[index].style.display = "block";
            if (ffobj[index].innerHTML == "默认地址") {
                ffobj[index].style.opacity = "1";
            } else {
                ffobj[index].style.opacity = "0.5";
            }
        }
        item.onmouseout = function () {
            ffobj[index].style.display = "none";
        }
    })
    //每个点击设置更改背景图，并请求ajax 修改flag标志
    ffobj.forEach((item, index) => {
        item.onclick = function () {
            console.log("每个点击设置更改背景图，并请求ajax 修改flag标志123")
            let that = this;
            ffobj.forEach((item, index2) => {
                ffobj[index2].style.display = "none";
                addDivobj[index2].className = "addDiv"
                ffobj[index2].className = "ff"
                ffobj[index2].innerHTML = "设为默认地址"
            })
            addDivobj[index].className = "addDiv addSel"
            ffobj[index].className = "ff ffid"
            ffobj[index].innerHTML = "默认地址"
            ffobj[index].style.opacity = "1";
            // 请求ajax 修改flag标志，目的：设置成默认地址
            async function modify(index) {
                console.log(1)
                let p = await ajax1Promise(`http://localhost:3000/ModifyData?index=${index}`);

            }
            modify(index)

        }
    })
}


// 初始省份的数据
async function getshengdataFN() {
    var obj_sheng = ["上海省", "安徽省", "江西省", "陕西省"]
    console.log("初始省份的数据")
    obj_sheng.forEach((item, index) => {
        var optionObj = document.createElement('option');
        optionObj.value = index;
        optionObj.innerText = item;
        document.querySelector("#sheng").appendChild(optionObj);
    })
}


